/*
 * PopTart - the "toaster pop" messages that scroll in
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import flash.display.Bitmap;
import flash.Lib;
import flash3f.BitFont;
import flash3f.InputHandler;

class PopTart extends Bitmap
// "toaster-pop" messages
// they move their position to accomodate the cursor
{

	static public var poplist : Array<PopTart>;
	public var pos : Int;
	public var waittime : Int;
	public var message : Array<Dynamic>;
	public var waitforever : Bool;
	static public var posattop : Bool = true;
	
	public function new(msg : Array<Dynamic>)
	{
		super();
		pos = 0;
		waittime = 10*msg.length; // 1/3 of a second per character
		waitforever = false;
		message = msg;
		bitmapData = BitFont.gen(msg);
		for (n in poplist) // surpress other pops
		{
			n.waittime=0;
			n.waitforever=false;
		}
		poplist.push(this);
		x = (680/2)-(bitmapData.width)/2;
		Lib.current.addChild(this);
		upme();		
	}

	public function upme()
	{
		if (pos>=48 && waittime>0)
		{
			if (!waitforever)
				waittime-=1;
		}
		else if (waittime>0)
		{
			pos+=2;
		}
		else
		{
			pos-=2;
		}
		
		if (pos<0)
		{
			poplist.remove(this);
			Lib.current.removeChild(this);			
		}
		
		if (posattop)
		{	
			y = -26 + pos;
		}
		else
		{
			y = -pos + 425;
		}
		
	}
		
	static public function up()
	{

		// if the mouse cursor moves strongly towards the top or bottom, adjust the popup
		// this implements a "neutral zone" where it's not changed if you go only slightly beyond halfway
		if (InputHandler.cursy>(425/2)+(425/4) && !posattop)
		{	
			posattop = true;
		}
		else if (InputHandler.cursy<(425/2)-(425/4) && posattop)
		{
			posattop = false;
		}
	
		for (n in poplist)
			n.upme();
	}
	
	static public function start()
	{
		PopTart.poplist = new Array();
	}
	
	static public function end()
	{
	}
	
}
